package hzh;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
@WebServlet(name="AddUserMesServlet")
public class AddUserMesServlet extends HttpServlet{

    private static final long serialVersionUID = 1139773442711986380L;
    private static final String INSERT_TEMPLATE =
            "INSERT INTO users (user_id, user_password) " +
                    "VALUES ('%s', '%s')";
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("user_id");//.request.getParameter()方法传递的数据，
        String password = request.getParameter("user_password");
        String sql = String.format(INSERT_TEMPLATE, name, password);
        //String.format为生成一个列表
        System.out.println(sql);
        try {
            DBUtils.insert(sql);//准备插入表单信息
            response.sendRedirect("./list1.jsp");//跳转到testlist界面
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void closeCon(Connection con) throws Exception{
        if(con!=null){
            con.close();
        }
    }
}
